/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectionSettingsFrame.java
 *
 * Created on 2010-08-17, 20:38:09
 */

package nsdh.view;
import javax.swing.*;
import java.util.*;
import nsdh.*;
import javax.swing.table.*;
import nsdh.model.script_models.*;


/**
 *
 * @author vtq
 */
public class EdgeSettingsFrame extends javax.swing.JFrame {

    

    /** Creates new form ConnectionSettingsFrame */
    public EdgeSettingsFrame(NsdhController nsdhController, GraphPanel graphPanel, String edgeType, String edgeName, String sourceCellType, String sourceCellName,  String targetCellType, String targetCellName) {
        initComponents();
        this.nsdhController = nsdhController;
        this.graphPanel = graphPanel;
        this.edgeType=edgeType ;
        this.edgeName=edgeName;
        this.sourceCellType=sourceCellType ;
        this.sourceCellName=sourceCellName;
        this.targetCellType=targetCellType;
        this.targetCellName=targetCellName;
        createParamsTableFields(edgeType,edgeName);
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        connectionTypeCombo = new javax.swing.JComboBox();
        connectionTypeLabel = new javax.swing.JLabel();
        connectionParamsPanel = new javax.swing.JPanel();
        connectionParamsScrollPane = new javax.swing.JScrollPane();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ustawienia krawedzi grafu");
        setBounds(new java.awt.Rectangle(100, 50, 0, 0));

        connectionTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Polaczenie serwer-klient", "Lacze standardowe", "Lacze Edge-Core", "Lacze Core-Core" }));
        connectionTypeCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectionTypeComboActionPerformed(evt);
            }
        });

        connectionTypeLabel.setText("Typ krawedzi:");

        connectionParamsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Parametry"));

        javax.swing.GroupLayout connectionParamsPanelLayout = new javax.swing.GroupLayout(connectionParamsPanel);
        connectionParamsPanel.setLayout(connectionParamsPanelLayout);
        connectionParamsPanelLayout.setHorizontalGroup(
            connectionParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectionParamsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(connectionParamsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                .addContainerGap())
        );
        connectionParamsPanelLayout.setVerticalGroup(
            connectionParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectionParamsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(connectionParamsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addContainerGap())
        );

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Anuluj");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(connectionParamsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(connectionTypeLabel)
                                .addGap(18, 18, 18)
                                .addComponent(connectionTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connectionTypeLabel)
                    .addComponent(connectionTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(connectionParamsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:

        try{

            //koncz edycje komorek jesli jakies sa edytowane
            if(connectionParamsTable.getCellEditor()!=null){
                connectionParamsTable.getCellEditor().stopCellEditing();
            }

            graphPanel.saveCellsToNsdhModel();

            if(connectionTypeCombo.getSelectedIndex()==0){

                //utworz tymczasowy obiekt
                Server_client_connection tempEdge = new Server_client_connection();

                //sprawdz czy nazwa sie nie powtarza
                if( !((String)connectionParamsTable.getValueAt(0, 1)).equals(edgeName) && nsdhController.nsdhModel.network_structure.server_client_connection_list.containsKey((String)connectionParamsTable.getValueAt(0, 1))){
                    throw new Exception("Krawedz o takiej nazwie juz istnieje, zmien nazwe.");
                }
                else if(connectionParamsTable.getValueAt(0, 1)==null || ((String)connectionParamsTable.getValueAt(0, 1)).equals("")){
                    throw new Exception("Nazwa krawedzi nie moze byc pusta");
                }
                else{
                    tempEdge.name = (String)connectionParamsTable.getValueAt(0, 1);
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.pc_list.containsKey((String)connectionParamsTable.getValueAt(1, 1)) ){
                    tempEdge.server_pc = (String)connectionParamsTable.getValueAt(1, 1);
                }else{
                    throw new Exception("Wybrany/wpisany serwer nie znajduje sie na liscie PC.");
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.pc_list.containsKey((String)connectionParamsTable.getValueAt(2, 1)) ){
                    tempEdge.client_pc = (String)connectionParamsTable.getValueAt(2, 1);
                }else{
                    throw new Exception("Wybrany/wpisany klient nie znajduje sie na liscie PC.");
                }


                //sprawdz czy krawedz juz nie istnieje miedzy wezlami
                if(((edgeName==null || edgeName.equals("")) && nsdhController.getExistingServerClientConnections(tempEdge.server_pc, tempEdge.client_pc).size()>0) ||
                        edgeName!=null && !edgeName.equals("") && nsdhController.getExistingServerClientConnections(tempEdge.server_pc, tempEdge.client_pc).size()>1){
                    throw new Exception("Krawedz miedzy wybranymi wezlami juz istnieje");
                }



                //sprawdz czy wybrana usluga znajduje sie na liscie
                if(nsdhController.nsdhModel.network_settings.service_list.containsKey((String)connectionParamsTable.getValueAt(3, 1)) ){
                    tempEdge.service = (String)connectionParamsTable.getValueAt(3, 1);
                }else{
                    throw new Exception("Wybrana/wpisana usluga nie znajduje sie na liscie uslug.");
                }

                //usun stara krawedz w zaleznosci od rodzaju poprzedniej krawedzi
                if(edgeType!=null && edgeType.equals("server_client_connection") && nsdhController.nsdhModel.network_structure.server_client_connection_list.containsKey(edgeName)){
                    //zamien nazwe starego obiektu, gdy wczesniej bylo to samo zaznaczone
                    nsdhController.RenameServerClientConnection(edgeName, tempEdge.name);
                    //nsdhController.nsdhModel.network_structure.server_client_connection_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("standard_link") && nsdhController.nsdhModel.network_structure.standard_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.standard_link_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("edge_core_link") && nsdhController.nsdhModel.network_structure.edge_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.edge_core_link_list.remove(edgeName);
                }else if(edgeType!=null && edgeType.equals("core_core_link") && nsdhController.nsdhModel.network_structure.core_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.core_core_link_list.remove(edgeName);
                }


                nsdhController.nsdhModel.network_structure.server_client_connection_list.put(tempEdge.name, tempEdge);
                nsdhController.nsdhModel.sequences.serverClientConnectionSequence++;


            }else if(connectionTypeCombo.getSelectedIndex()==1){ //gdy wybrany stabdard_link

                //utworz tymczasowy obiekt
                Standard_link tempEdge = new Standard_link();

                //sprawdz czy nazwa sie nie powtarza
                if( !((String)connectionParamsTable.getValueAt(0, 1)).equals(edgeName) && nsdhController.nsdhModel.network_structure.standard_link_list.containsKey((String)connectionParamsTable.getValueAt(0, 1))){
                    throw new Exception("Krawedz o takiej nazwie juz istnieje, zmien nazwe.");
                }
                else if(connectionParamsTable.getValueAt(0, 1)==null || ((String)connectionParamsTable.getValueAt(0, 1)).equals("")){
                    throw new Exception("Nazwa krawedzi nie moze byc pusta");
                }
                else{
                    tempEdge.name = (String)connectionParamsTable.getValueAt(0, 1);
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.pc_list.containsKey((String)connectionParamsTable.getValueAt(1, 1)) || nsdhController.nsdhModel.network_structure.router_list.containsKey((String)connectionParamsTable.getValueAt(1, 1))){
                    tempEdge.node1 = (String)connectionParamsTable.getValueAt(1, 1);
                }else{
                    throw new Exception("Wybrany/wpisany node1 nie znajduje sie na liscie PC i routerow.");
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.pc_list.containsKey((String)connectionParamsTable.getValueAt(2, 1)) || nsdhController.nsdhModel.network_structure.router_list.containsKey((String)connectionParamsTable.getValueAt(2, 1))){
                    tempEdge.node2 = (String)connectionParamsTable.getValueAt(2, 1);
                }else{
                    throw new Exception("Wybrany/wpisany node2 nie znajduje sie na liscie PC i routerow.");
                }

                //sprawdz czy krawedz juz nie istnieje miedzy wezlami
                if(((edgeName==null || edgeName.equals("")) && nsdhController.getExistingLinks(tempEdge.node1, tempEdge.node2).size()>0) ||
                        edgeName!=null && !edgeName.equals("") && nsdhController.getExistingLinks(tempEdge.node1, tempEdge.node2).size()>1){
                    throw new Exception("Krawedz miedzy wybranymi wezlami juz istnieje");
                }


                //sprawdz czy wybrany parametr to liczba double i nie jest pusta
                if(connectionParamsTable.getValueAt(3, 1)!=null && !((String)connectionParamsTable.getValueAt(3, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(3, 1));
                        tempEdge.delay = (String)connectionParamsTable.getValueAt(3, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole delay musi byc liczba.");
                    }
                }
                else {
                    throw new Exception("Pole delay nie moze byc puste.");
                }


                //sprawdz czy wybrany parametr to liczba double i nie jest pusta
                if(connectionParamsTable.getValueAt(4, 1)!=null && !((String)connectionParamsTable.getValueAt(4, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(4, 1));
                        tempEdge.bandwidth = (String)connectionParamsTable.getValueAt(4, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole bandwidth musi byc liczba.");
                    }
                }
                else {
                    throw new Exception("Pole bandwidth nie moze byc puste.");
                }

                //sprawdz czy wybrany parametr to liczba double
                 if(connectionParamsTable.getValueAt(5, 1)!=null && !((String)connectionParamsTable.getValueAt(5, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(5, 1));
                        tempEdge.queue_limit = (String)connectionParamsTable.getValueAt(5, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole queue_limit musi byc liczba.");
                    }
                 }else{
                    tempEdge.queue_limit = "";
                 }
                


                //usun stara krawedz w zaleznosci od rodzaju poprzedniej krawedzi
                if(edgeType!=null && edgeType.equals("server_client_connection") && nsdhController.nsdhModel.network_structure.server_client_connection_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.DeleteServerClientConnection(edgeName);
                    //nsdhController.nsdhModel.network_structure.server_client_connection_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("standard_link") && nsdhController.nsdhModel.network_structure.standard_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.standard_link_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("edge_core_link") && nsdhController.nsdhModel.network_structure.edge_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.edge_core_link_list.remove(edgeName);
                }else if(edgeType!=null && edgeType.equals("core_core_link") && nsdhController.nsdhModel.network_structure.core_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.core_core_link_list.remove(edgeName);
                }


                nsdhController.nsdhModel.network_structure.standard_link_list.put(tempEdge.name, tempEdge);
                nsdhController.nsdhModel.sequences.standardLinkSequence++;

            }else if(connectionTypeCombo.getSelectedIndex()==2){
                //gdy wybrany jest edge_core_link


                //utworz tymczasowy obiekt
                Edge_core_link tempEdge = new Edge_core_link();

                //sprawdz czy nazwa sie nie powtarza
                if( !((String)connectionParamsTable.getValueAt(0, 1)).equals(edgeName) && nsdhController.nsdhModel.network_structure.edge_core_link_list.containsKey((String)connectionParamsTable.getValueAt(0, 1))){
                    throw new Exception("Krawedz o takiej nazwie juz istnieje, zmien nazwe.");
                }
                else if(connectionParamsTable.getValueAt(0, 1)==null || ((String)connectionParamsTable.getValueAt(0, 1)).equals("")){
                    throw new Exception("Nazwa krawedzi nie moze byc pusta");
                }
                else{
                    tempEdge.name = (String)connectionParamsTable.getValueAt(0, 1);
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_settings.queue_policy_list.containsKey((String)connectionParamsTable.getValueAt(1, 1))){
                    tempEdge.queue_policy = (String)connectionParamsTable.getValueAt(1, 1);
                }else{
                    throw new Exception("Wybrana queue_policy nie znajduje sie na liscie polityk.");
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.router_list.containsKey((String)connectionParamsTable.getValueAt(2, 1))){
                    tempEdge.edge_router = (String)connectionParamsTable.getValueAt(2, 1);
                }else{
                    throw new Exception("Wybrany/wpisany edge_router nie znajduje sie na liscie routerow.");
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.router_list.containsKey((String)connectionParamsTable.getValueAt(3, 1))){
                    tempEdge.core_router = (String)connectionParamsTable.getValueAt(3, 1);
                }else{
                    throw new Exception("Wybrany/wpisany core_router nie znajduje sie na liscie routerow.");
                }


                //sprawdz czy krawedz juz nie istnieje miedzy wezlami
                if(((edgeName==null || edgeName.equals("")) && nsdhController.getExistingLinks(tempEdge.edge_router, tempEdge.core_router).size()>0) ||
                        edgeName!=null && !edgeName.equals("") && nsdhController.getExistingLinks(tempEdge.edge_router, tempEdge.core_router).size()>1){
                    throw new Exception("Krawedz miedzy wybranymi wezlami juz istnieje");
                }

                //sprawdz czy wybrany parametr to liczba double i nie jest pusta
                if(connectionParamsTable.getValueAt(4, 1)!=null && !((String)connectionParamsTable.getValueAt(4, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(4, 1));
                        tempEdge.delay = (String)connectionParamsTable.getValueAt(4, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole delay musi byc liczba.");
                    }
                }
                else {
                    throw new Exception("Pole delay nie moze byc puste.");
                }


                //sprawdz czy wybrany parametr to liczba double i nie jest pusta
                if(connectionParamsTable.getValueAt(5, 1)!=null && !((String)connectionParamsTable.getValueAt(5, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(5, 1));
                        tempEdge.bandwidth = (String)connectionParamsTable.getValueAt(5, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole bandwidth musi byc liczba.");
                    }
                }
                else {
                    throw new Exception("Pole bandwidth nie moze byc puste.");
                }

                //sprawdz czy wybrany parametr to liczba double
                 if(connectionParamsTable.getValueAt(6, 1)!=null && !((String)connectionParamsTable.getValueAt(6, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(6, 1));
                        tempEdge.queue_limit = (String)connectionParamsTable.getValueAt(6, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole queue_limit musi byc liczba.");
                    }
                 }else{
                    tempEdge.queue_limit = "";
                 }
                


                //usun stara krawedz w zaleznosci od rodzaju poprzedniej krawedzi
                if(edgeType!=null && edgeType.equals("server_client_connection") && nsdhController.nsdhModel.network_structure.server_client_connection_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.DeleteServerClientConnection(edgeName);
                    //nsdhController.nsdhModel.network_structure.server_client_connection_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("standard_link") && nsdhController.nsdhModel.network_structure.standard_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.standard_link_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("edge_core_link") && nsdhController.nsdhModel.network_structure.edge_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.edge_core_link_list.remove(edgeName);
                }else if(edgeType!=null && edgeType.equals("core_core_link") && nsdhController.nsdhModel.network_structure.core_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.core_core_link_list.remove(edgeName);
                }


                nsdhController.nsdhModel.network_structure.edge_core_link_list.put(tempEdge.name, tempEdge);
                nsdhController.nsdhModel.sequences.edgeCoreLinkSequence++;



            }else if(connectionTypeCombo.getSelectedIndex()==3){
                //gdy wybrany jest edge_core_link


                //utworz tymczasowy obiekt
                Core_core_link tempEdge = new Core_core_link();

                //sprawdz czy nazwa sie nie powtarza
                if( !((String)connectionParamsTable.getValueAt(0, 1)).equals(edgeName) && nsdhController.nsdhModel.network_structure.core_core_link_list.containsKey((String)connectionParamsTable.getValueAt(0, 1))){
                    throw new Exception("Krawedz o takiej nazwie juz istnieje, zmien nazwe.");
                }
                else if(connectionParamsTable.getValueAt(0, 1)==null || ((String)connectionParamsTable.getValueAt(0, 1)).equals("")){
                    throw new Exception("Nazwa krawedzi nie moze byc pusta");
                }
                else{
                    tempEdge.name = (String)connectionParamsTable.getValueAt(0, 1);
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_settings.queue_policy_list.containsKey((String)connectionParamsTable.getValueAt(1, 1))){
                    tempEdge.queue_policy = (String)connectionParamsTable.getValueAt(1, 1);
                }else{
                    throw new Exception("Wybrana queue_policy nie znajduje sie na liscie polityk.");
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.router_list.containsKey((String)connectionParamsTable.getValueAt(2, 1))){
                    tempEdge.core_router1 = (String)connectionParamsTable.getValueAt(2, 1);
                }else{
                    throw new Exception("Wybrany/wpisany core_router1 nie znajduje sie na liscie routerow.");
                }

                //sprawdz czy wybrany wezel znajduje sie na liscie
                if(nsdhController.nsdhModel.network_structure.router_list.containsKey((String)connectionParamsTable.getValueAt(3, 1))){
                    tempEdge.core_router2 = (String)connectionParamsTable.getValueAt(3, 1);
                }else{
                    throw new Exception("Wybrany/wpisany core_router2 nie znajduje sie na liscie routerow.");
                }

                //sprawdz czy krawedz juz nie istnieje miedzy wezlami
                if(((edgeName==null || edgeName.equals("")) && nsdhController.getExistingLinks(tempEdge.core_router1, tempEdge.core_router2).size()>0) ||
                        edgeName!=null && !edgeName.equals("") && nsdhController.getExistingLinks(tempEdge.core_router1, tempEdge.core_router2).size()>1){
                    throw new Exception("Krawedz miedzy wybranymi wezlami juz istnieje");
                }

                //sprawdz czy wybrany parametr to liczba double i nie jest pusta
                if(connectionParamsTable.getValueAt(4, 1)!=null && !((String)connectionParamsTable.getValueAt(4, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(4, 1));
                        tempEdge.delay = (String)connectionParamsTable.getValueAt(4, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole delay musi byc liczba.");
                    }
                }
                else {
                    throw new Exception("Pole delay nie moze byc puste.");
                }


                //sprawdz czy wybrany parametr to liczba double i nie jest pusta
                if(connectionParamsTable.getValueAt(5, 1)!=null && !((String)connectionParamsTable.getValueAt(5, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(5, 1));
                        tempEdge.bandwidth = (String)connectionParamsTable.getValueAt(5, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole bandwidth musi byc liczba.");
                    }
                }
                else {
                    throw new Exception("Pole bandwidth nie moze byc puste.");
                }

                //sprawdz czy wybrany parametr to liczba double
                 if(connectionParamsTable.getValueAt(6, 1)!=null && !((String)connectionParamsTable.getValueAt(6, 1)).equals("")){
                    try{
                        Double.parseDouble((String)connectionParamsTable.getValueAt(6, 1));
                        tempEdge.queue_limit = (String)connectionParamsTable.getValueAt(6, 1);
                    }
                    catch(Exception e){
                        throw new Exception("Pole queue_limit musi byc liczba.");
                    }
                 }else{
                    tempEdge.queue_limit = "";
                 }



                //usun stara krawedz w zaleznosci od rodzaju poprzedniej krawedzi
                if(edgeType!=null && edgeType.equals("server_client_connection") && nsdhController.nsdhModel.network_structure.server_client_connection_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.DeleteServerClientConnection(edgeName);
                    //nsdhController.nsdhModel.network_structure.server_client_connection_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("standard_link") && nsdhController.nsdhModel.network_structure.standard_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.standard_link_list.remove(edgeName);
                }
                else if(edgeType!=null && edgeType.equals("edge_core_link") && nsdhController.nsdhModel.network_structure.edge_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.edge_core_link_list.remove(edgeName);
                }else if(edgeType!=null && edgeType.equals("core_core_link") && nsdhController.nsdhModel.network_structure.core_core_link_list.containsKey(edgeName)){
                    //usun stary obiekt z modelu
                    nsdhController.nsdhModel.network_structure.core_core_link_list.remove(edgeName);
                }


                nsdhController.nsdhModel.network_structure.core_core_link_list.put(tempEdge.name, tempEdge);
                nsdhController.nsdhModel.sequences.coreCoreLinkSequence++;
            }




           


           graphPanel.loadEdgesFromNsdhModel();
           dispose();


            //zwieksz numer sekwencyjny
            
        
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getMessage(), "Ostrzeżenie", JOptionPane.WARNING_MESSAGE);
        }




    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * Ustawia Parametry w tabeli w zaleznosci od wybranej opcji rodzaju krawedzi w checkbox
     * @param evt
     */
    private void connectionTypeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectionTypeComboActionPerformed
        // TODO add your handling code here:
        if(connectionTypeCombo.getSelectedIndex()==0){
            if(edgeType!=null && edgeType.equals("server_client_connection")){
                createParamsTableFields("server_client_connection",edgeName);
            }else{
                createParamsTableFields("server_client_connection",null);
            }
            
        }else if(connectionTypeCombo.getSelectedIndex()==1){
            if(edgeType!=null && edgeType.equals("standard_link")){
                createParamsTableFields("standard_link",edgeName);
            }else{
                createParamsTableFields("standard_link",null);
            }
        }else if(connectionTypeCombo.getSelectedIndex()==2){
            if(edgeType!=null && edgeType.equals("edge_core_link")){
                createParamsTableFields("edge_core_link",edgeName);
            }else{
                createParamsTableFields("edge_core_link",null);
            }
        }else if(connectionTypeCombo.getSelectedIndex()==3){
            if(edgeType!=null && edgeType.equals("core_core_link")){
                createParamsTableFields("core_core_link",edgeName);
            }else{
                createParamsTableFields("core_core_link",null);
            }
        }


    }//GEN-LAST:event_connectionTypeComboActionPerformed

    /**
     * Tworzy pola tabeli parametrow w zaleznosci od typu krawedzi
     * @param edgeType typ krawedzi
     * @param edgeName nazwa krawedzi ktorej wartosci wczytac z modelu nsdhModel, jesli null - puste wartosci
     */
    private void createParamsTableFields(String edgeType, String edgeName){

        //domyslnie ustaw server_client_connection jako edgetype
        if(edgeType==null){
            createParamsTableFields("server_client_connection",null);
        }
        //dla typu server_client_connection
        else if(edgeType.equals("server_client_connection")){

            //ustaw combox typu krawedzi
            connectionTypeCombo.setSelectedItem(0);

            //tablica PC na grafie
            Set<String> pcNameSet = new HashSet(nsdhController.nsdhModel.network_structure.pc_list.keySet());

            pcNameSet.add("");
            Object[] pcNameArray = pcNameSet.toArray();
            java.util.Arrays.sort(pcNameArray);

            //utworz combobox serverpc i clientpc
            JComboBox serverPcComboBox = new JComboBox();
            serverPcComboBox.setModel(new javax.swing.DefaultComboBoxModel(pcNameArray));

            JComboBox clientPcComboBox = new JComboBox();
            clientPcComboBox.setModel(new javax.swing.DefaultComboBoxModel(pcNameArray));

            //tablica i combobox uslug
            Set<String> serviceNameSet = new HashSet(nsdhController.nsdhModel.network_settings.service_list.keySet());
            serviceNameSet.add("");
            Object[] serviceNameArray = serviceNameSet.toArray();
            java.util.Arrays.sort(serviceNameArray);

            JComboBox serviceComboBox = new JComboBox();
            serviceComboBox.setModel(new javax.swing.DefaultComboBoxModel(serviceNameArray));


            String[] col_names = {"Nazwa", "Wartosc"};

            DefaultTableModel tableModel = new DefaultTableModel(col_names,4)
            {
                public String[] prop_names = { "name", "server_pc", "client_pc",
                                               "service" };
                public Object getValueAt(int row, int col)
                {
                    if (col==0)
                        return prop_names[row];
                    return super.getValueAt(row,col);
                }
                public boolean isCellEditable(int row, int col)
                {
                    if (col==0)
                        return false;
                    return true;
                }
            };

            connectionParamsTable = new JTableX(tableModel);

            // utworz edytor dla wierszy RowEditorModel
            RowEditorModel rm = new RowEditorModel();
            // powiedz tabeli jakiego modelu edytora bedziemy uzywac
            connectionParamsTable.setRowEditorModel(rm);

            //ustaw edytor dla kazdej z kolumn
            DefaultCellEditor serverPcEd = new DefaultCellEditor(serverPcComboBox);
            rm.addEditorForRow(1,serverPcEd);

            DefaultCellEditor clientPcEd = new DefaultCellEditor(clientPcComboBox);
            rm.addEditorForRow(2,clientPcEd);

            DefaultCellEditor serviceEd = new DefaultCellEditor(serviceComboBox);
            rm.addEditorForRow(3,serviceEd);

            if(edgeName!=null){
                connectionParamsTable.setValueAt(edgeName, 0,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.server_client_connection_list.get(edgeName).server_pc, 1,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.server_client_connection_list.get(edgeName).client_pc, 2,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.server_client_connection_list.get(edgeName).service, 3,1);

                //ustaw domyslne pola wierzcholkow
                sourceCellName = nsdhController.nsdhModel.network_structure.server_client_connection_list.get(edgeName).server_pc;
                targetCellName = nsdhController.nsdhModel.network_structure.server_client_connection_list.get(edgeName).client_pc;
                
            }
            else{
                connectionParamsTable.setValueAt("ServerClientConnection"+nsdhController.nsdhModel.sequences.serverClientConnectionSequence, 0,1);
                connectionParamsTable.setValueAt(sourceCellName, 1,1);
                connectionParamsTable.setValueAt(targetCellName, 2,1);
            }

        }else if(edgeType.equals("standard_link")){
            connectionTypeCombo.setSelectedIndex(1);




            //tablica PC i routerow na grafie
            Set<String> cellNameSet = new HashSet(nsdhController.nsdhModel.network_structure.pc_list.keySet());
            


            cellNameSet.addAll(nsdhController.nsdhModel.network_structure.router_list.keySet());
            cellNameSet.add("");
            Object[] cellNameArray = cellNameSet.toArray();
            java.util.Arrays.sort(cellNameArray);

            //utworz combobox serverpc i clientpc
            JComboBox node1ComboBox = new JComboBox();
            node1ComboBox.setModel(new javax.swing.DefaultComboBoxModel(cellNameArray));

            JComboBox node2ComboBox = new JComboBox();
            node2ComboBox.setModel(new javax.swing.DefaultComboBoxModel(cellNameArray));

            


            String[] col_names = {"Nazwa", "Wartosc"};

            DefaultTableModel tableModel = new DefaultTableModel(col_names,6)
            {
                public String[] prop_names = { "name", "node1", "node2",
                                               "delay[ms]","bandwidth[Mb]","queue_limit" };
                public Object getValueAt(int row, int col)
                {
                    if (col==0)
                        return prop_names[row];
                    return super.getValueAt(row,col);
                }
                public boolean isCellEditable(int row, int col)
                {
                    if (col==0)
                        return false;
                    return true;
                }
            };

            connectionParamsTable = new JTableX(tableModel);

            // create a RowEditorModel... this is used to hold the extra
            // information that is needed to deal with row specific editors
            RowEditorModel rm = new RowEditorModel();
            // tell the JTableX which RowEditorModel we are using
            connectionParamsTable.setRowEditorModel(rm);

            //ustaw edytor dla kazdej z kolumn
            DefaultCellEditor serverPcEd = new DefaultCellEditor(node1ComboBox);
            rm.addEditorForRow(1,serverPcEd);

            DefaultCellEditor clientPcEd = new DefaultCellEditor(node2ComboBox);
            rm.addEditorForRow(2,clientPcEd);

            if(edgeName!=null){
                connectionParamsTable.setValueAt(edgeName, 0,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.standard_link_list.get(edgeName).node1, 1,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.standard_link_list.get(edgeName).node2, 2,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.standard_link_list.get(edgeName).delay, 3,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.standard_link_list.get(edgeName).bandwidth, 4,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.standard_link_list.get(edgeName).queue_limit, 5,1);

                //ustaw domysle wartosci wierzcholkow
                sourceCellName = nsdhController.nsdhModel.network_structure.standard_link_list.get(edgeName).node1;
                targetCellName = nsdhController.nsdhModel.network_structure.standard_link_list.get(edgeName).node2;
            }
            else{
                connectionParamsTable.setValueAt("StandardLink"+nsdhController.nsdhModel.sequences.standardLinkSequence, 0,1);
                connectionParamsTable.setValueAt(sourceCellName, 1,1);
                connectionParamsTable.setValueAt(targetCellName, 2,1);
            }





            
        }else if(edgeType.equals("edge_core_link")){
            connectionTypeCombo.setSelectedIndex(2);


            //tablica routerow na grafie
            Set<String> cellNameSet = new HashSet(nsdhController.nsdhModel.network_structure.router_list.keySet());
            cellNameSet.add(""); //pierwsze puste pole
            Object[] cellNameArray = cellNameSet.toArray();
            java.util.Arrays.sort(cellNameArray); //sortowanie

            //utworz comboboxy routerow edge i core
            JComboBox edgeRouterComboBox = new JComboBox();
            edgeRouterComboBox.setModel(new javax.swing.DefaultComboBoxModel(cellNameArray));

            JComboBox coreRouterComboBox = new JComboBox();
            coreRouterComboBox.setModel(new javax.swing.DefaultComboBoxModel(cellNameArray));


            //tablica dostepnych polityk kolejkowania
            Set<String> queuePolicyNameSet = new HashSet(nsdhController.nsdhModel.network_settings.queue_policy_list.keySet());
            queuePolicyNameSet.add("");
            Object[] queuePolicyNameArray = queuePolicyNameSet.toArray();
            java.util.Arrays.sort(queuePolicyNameArray);

            //utworz combobox polityk kolejkowania
            JComboBox queuePolicyComboBox = new JComboBox();
            queuePolicyComboBox.setModel(new javax.swing.DefaultComboBoxModel(queuePolicyNameArray));


            String[] col_names = {"Nazwa", "Wartosc"};

            DefaultTableModel tableModel = new DefaultTableModel(col_names,7)
            {
                public String[] prop_names = { "name", "queue_policy","edge_router", "core_router",
                                               "delay[ms]","bandwidth[Mb]","queue_limit" };
                public Object getValueAt(int row, int col)
                {
                    if (col==0)
                        return prop_names[row];
                    return super.getValueAt(row,col);
                }
                public boolean isCellEditable(int row, int col)
                {
                    if (col==0)
                        return false;
                    return true;
                }
            };

            connectionParamsTable = new JTableX(tableModel);

            // create a RowEditorModel... this is used to hold the extra
            // information that is needed to deal with row specific editors
            RowEditorModel rm = new RowEditorModel();
            // tell the JTableX which RowEditorModel we are using
            connectionParamsTable.setRowEditorModel(rm);

            //ustaw edytor combox dla kazdej z kolumn

            DefaultCellEditor queuePolicyEd = new DefaultCellEditor(queuePolicyComboBox);
            rm.addEditorForRow(1,queuePolicyEd);

            DefaultCellEditor edgeRouterEd = new DefaultCellEditor(edgeRouterComboBox);
            rm.addEditorForRow(2,edgeRouterEd);

            DefaultCellEditor coreRouterEd = new DefaultCellEditor(coreRouterComboBox);
            rm.addEditorForRow(3,coreRouterEd);

            //jesli pobieramy dane z nsdhmodel
            if(edgeName!=null){
                connectionParamsTable.setValueAt(edgeName, 0,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).queue_policy, 1,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).edge_router, 2,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).core_router, 3,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).delay, 4,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).bandwidth, 5,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).queue_limit, 6,1);

                //ustaw domyslne wierzcholki
                sourceCellName=nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).edge_router;
                targetCellName=nsdhController.nsdhModel.network_structure.edge_core_link_list.get(edgeName).core_router;
            }
            else{ //gdy tworzymy nowy obiekt
                connectionParamsTable.setValueAt("EdgeCoreLink"+nsdhController.nsdhModel.sequences.edgeCoreLinkSequence, 0,1);
                connectionParamsTable.setValueAt(sourceCellName, 2,1);
                connectionParamsTable.setValueAt(targetCellName, 3,1);
            }


        }else if(edgeType.equals("core_core_link")){
            connectionTypeCombo.setSelectedIndex(3);

            //tablica routerow na grafie
            Set<String> cellNameSet = new HashSet(nsdhController.nsdhModel.network_structure.router_list.keySet());
            cellNameSet.add(""); //pierwsze puste pole
            Object[] cellNameArray = cellNameSet.toArray();
            java.util.Arrays.sort(cellNameArray); //sortowanie

            //utworz comboboxy routerow edge i core
            JComboBox coreRouter1ComboBox = new JComboBox();
            coreRouter1ComboBox.setModel(new javax.swing.DefaultComboBoxModel(cellNameArray));

            JComboBox coreRouter2ComboBox = new JComboBox();
            coreRouter2ComboBox.setModel(new javax.swing.DefaultComboBoxModel(cellNameArray));


            //tablica dostepnych polityk kolejkowania
            Set<String> queuePolicyNameSet = new HashSet(nsdhController.nsdhModel.network_settings.queue_policy_list.keySet());
            queuePolicyNameSet.add("");
            Object[] queuePolicyNameArray = queuePolicyNameSet.toArray();
            java.util.Arrays.sort(queuePolicyNameArray);

            //utworz combobox polityk kolejkowania
            JComboBox queuePolicyComboBox = new JComboBox();
            queuePolicyComboBox.setModel(new javax.swing.DefaultComboBoxModel(queuePolicyNameArray));


            String[] col_names = {"Nazwa", "Wartosc"};

            DefaultTableModel tableModel = new DefaultTableModel(col_names,7)
            {
                public String[] prop_names = { "name", "queue_policy","core_router1", "core_router2",
                                               "delay[ms]","bandwidth[Mb]","queue_limit" };
                public Object getValueAt(int row, int col)
                {
                    if (col==0)
                        return prop_names[row];
                    return super.getValueAt(row,col);
                }
                public boolean isCellEditable(int row, int col)
                {
                    if (col==0)
                        return false;
                    return true;
                }
            };

            connectionParamsTable = new JTableX(tableModel);

            // create a RowEditorModel... this is used to hold the extra
            // information that is needed to deal with row specific editors
            RowEditorModel rm = new RowEditorModel();
            // tell the JTableX which RowEditorModel we are using
            connectionParamsTable.setRowEditorModel(rm);

            //ustaw edytor combox dla kazdej z kolumn

            DefaultCellEditor queuePolicyEd = new DefaultCellEditor(queuePolicyComboBox);
            rm.addEditorForRow(1,queuePolicyEd);

            DefaultCellEditor coreRouter1Ed = new DefaultCellEditor(coreRouter1ComboBox);
            rm.addEditorForRow(2,coreRouter1Ed);

            DefaultCellEditor coreRouter2Ed = new DefaultCellEditor(coreRouter2ComboBox);
            rm.addEditorForRow(3,coreRouter2Ed);

            //jesli pobieramy dane z nsdhmodel
            if(edgeName!=null){
                connectionParamsTable.setValueAt(edgeName, 0,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).queue_policy, 1,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).core_router1, 2,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).core_router2, 3,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).delay, 4,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).bandwidth, 5,1);
                connectionParamsTable.setValueAt(nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).queue_limit, 6,1);

                sourceCellName=nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).core_router1;
                targetCellName=nsdhController.nsdhModel.network_structure.core_core_link_list.get(edgeName).core_router2;
            }
            else{ //gdy tworzymy nowy obiekt
                connectionParamsTable.setValueAt("CoreCoreLink"+nsdhController.nsdhModel.sequences.coreCoreLinkSequence, 0,1);
                connectionParamsTable.setValueAt(sourceCellName, 2,1);
                connectionParamsTable.setValueAt(targetCellName, 3,1);
            }

        }

        connectionParamsTable.setRowSelectionAllowed(false);
        connectionParamsTable.setColumnSelectionAllowed(false);
        connectionParamsScrollPane.setViewportView(connectionParamsTable);
        pack();

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel connectionParamsPanel;
    private javax.swing.JScrollPane connectionParamsScrollPane;
    private javax.swing.JComboBox connectionTypeCombo;
    private javax.swing.JLabel connectionTypeLabel;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

    private NsdhController nsdhController;
    private GraphPanel graphPanel;
    private  JTableX connectionParamsTable;
    private String sourceCellType;
    private String sourceCellName;
    private String targetCellType;
    private String targetCellName;
    private String edgeType;
    private String edgeName;
}
